<html>
<head><meta charset="utf-8"><title>questions from Stack Overflow · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html">questions from Stack Overflow</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="161526724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161526724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161526724">(Mar 23 2019 at 13:50)</a>:</h4>
<p>Is it sound to transmute a MaybeUninit&lt;[T; N]&gt; to [MaybeUninit&lt;T&gt;; N]? (<a href="https://stackoverflow.com/q/55313460/155423" target="_blank" title="https://stackoverflow.com/q/55313460/155423">https://stackoverflow.com/q/55313460/155423</a>)</p>
<p>/cc <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="161526748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161526748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161526748">(Mar 23 2019 at 13:51)</a>:</h4>
<p>I think it's "yes", but I've spent all of 5 seconds thinking about it</p>



<a name="161527246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161527246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161527246">(Mar 23 2019 at 14:04)</a>:</h4>
<p>my rationale (as much as I have) is because it's a union</p>



<a name="161528611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161528611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161528611">(Mar 23 2019 at 14:45)</a>:</h4>
<p>With what we guarantee about arrays, this question boils down to "does <code>MaybeUninit&lt;T&gt;</code> have the same memory layout as <code>T</code>?". Everyone wants that but I don't think it is technically guaranteed yet.</p>



<a name="161528902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161528902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161528902">(Mar 23 2019 at 14:53)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> so my wishful "cause it's a <code>union</code>" holds no water? I guess I shouldn't be surprised.</p>



<a name="161528955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161528955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161528955">(Mar 23 2019 at 14:54)</a>:</h4>
<p>Well, MaybeUninit makes all the concerns about validity go away. But transmute also needs memory layout compatibility and that isn't specified for unions in general (only for <code>repr(C)</code> ones).</p>



<a name="161528972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161528972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161528972">(Mar 23 2019 at 14:55)</a>:</h4>
<p>Well, the validity of unions in general is also not settled, but the entire point of MaybeUninit is to side-step validity based pitfalls, so you'll be fine in that respect.</p>



<a name="161538823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161538823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161538823">(Mar 23 2019 at 19:11)</a>:</h4>
<p><code>#[repr(transparent)]</code> to the rescue?</p>



<a name="161629127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161629127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161629127">(Mar 25 2019 at 10:11)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> while that is not guaranteed, this is how it is actually implemented</p>



<a name="161629242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161629242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161629242">(Mar 25 2019 at 10:13)</a>:</h4>
<p><code>transmute</code> only works if both types have the same size - if the layout of the union were to change in such a way that this is broken, then I can't imagine a situation in which <code>transmute</code> wouldn't have to fail to compile</p>



<a name="161629269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161629269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161629269">(Mar 25 2019 at 10:14)</a>:</h4>
<p>since the field in the union must be properly aligned, so for the layout to change, the size would need to change</p>



<a name="161719355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161719355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161719355">(Mar 26 2019 at 00:43)</a>:</h4>
<p>The best way of resolving these kinds of questions is to put the relevant conversions in the standard library/compiler.</p>



<a name="161739758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161739758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161739758">(Mar 26 2019 at 08:04)</a>:</h4>
<p>These are unsafe conversions, I don't think we have a trait for this.</p>



<a name="161742856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161742856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161742856">(Mar 26 2019 at 09:04)</a>:</h4>
<p>Wait, I'm wrong, these aren't unsafe:</p>
<div class="codehilite"><pre><span></span>impl&lt;T, N: const usize&gt; MaybeUninit&lt;[T; N]&gt; {
    fn foo(self) -&gt; [MaybeUninit&lt;T&gt;; N];
}
impl&lt;T, N: const usize&gt; [MaybeUninit&lt;T&gt;; N] {
    fn bar(self) -&gt; MaybeUninit&lt;[T; N]&gt;;
}
</pre></div>


<p>so we can probably just use <code>From</code>/<code>Into</code> for these.</p>



<a name="161743747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161743747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161743747">(Mar 26 2019 at 09:18)</a>:</h4>
<p>yeah I think this transmute is fine. it's part of "interaction of <code>MaybeUninit</code> and arrays/slices" which we'll get to eventually, I hope</p>



<a name="161743769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161743769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161743769">(Mar 26 2019 at 09:19)</a>:</h4>
<blockquote>
<p>With what we guarantee about arrays, this question boils down to "does <code>MaybeUninit&lt;T&gt;</code> have the same memory layout as <code>T</code>?". Everyone wants that but I don't think it is technically guaranteed yet.</p>
</blockquote>
<p>I think while we do not guarantee it for unions the user writes, we do guarantee it for <code>MaybeUninit</code></p>



<a name="161743780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161743780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161743780">(Mar 26 2019 at 09:19)</a>:</h4>
<p><code>as_ptr</code> and the other ptr/ref-getters of <code>MaybeUninit</code> basically rely on this</p>



<a name="161749284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161749284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161749284">(Mar 26 2019 at 10:43)</a>:</h4>
<p>Technically the interface of those conversions could be implemented even if MaybeUninit had e.g. extra padding or larger alignment, right? They only need MaybeUninit to actually contain a T, not to contain <em>only</em> a T.</p>



<a name="161751873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161751873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161751873">(Mar 26 2019 at 11:25)</a>:</h4>
<p>hmm I am not sure</p>



<a name="161751897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161751897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161751897">(Mar 26 2019 at 11:25)</a>:</h4>
<p>if <code>MaybeUnit</code> had padding, then how do you get a pointer to its field without creating a reference ?</p>



<a name="161751967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161751967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161751967">(Mar 26 2019 at 11:26)</a>:</h4>
<p>right now all of this relies on "pointer to union == pointer to all fields", but that only holds if <code>MaybeUninit</code> is <code>repr(C)</code>, which it is not (and making it <code>repr(C)</code> runs into what do ZSTs do in <code>repr(C)</code>-type of issues)</p>



<a name="161787730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions%20from%20Stack%20Overflow/near/161787730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/questions.20from.20Stack.20Overflow.html#161787730">(Mar 26 2019 at 18:02)</a>:</h4>
<p>The implementation we have right now certainly assumes on "pointer to union == pointer to all fields" but that's not exposed by those interfaces (they could be implemented with unstable or rustc-specific intrinsics or knowledge, for example). None of this is intended to say that MaybeUninit should ever be layout <em>in</em>compatible, or that the implementation needs to be changes. I'm just saying, right now as an external user, you can't rules lawyer your way to the conclusion that MaybeUninit&lt;T&gt; has the same layout as T. We should fix that, of course.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>